package cn.openea.perpartner.cms.web.controller;

import cn.openea.perpartner.cms.annotation.CmsLog;
import cn.openea.perpartner.cms.enums.OperatorType;
import cn.openea.perpartner.cms.web.dto.AssignDto;
import cn.openea.perpartner.cms.web.dto.CommunityDto;
import cn.openea.perpartner.cms.web.dto.RandomDto;
import cn.openea.perpartner.cms.web.entity.CmsCommunity;
import cn.openea.perpartner.cms.web.service.ICmsCommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * (CmsCommunity)表控制层
 *
 * @author makejava
 * @since 2022-08-10 14:30:20
 */
@Api(tags = "团购分类")
@RestController
@RequestMapping("community")
public class CmsCommunityController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ICmsCommunityService communityService;



    @ApiOperation("客户列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String userId, String searchStr){
        startPage();
        List<CmsCommunity> list = communityService.selectList(userId,searchStr);
        return  getDataTable(list);
    }
    /**
     * 导入数据
     */
    @ApiOperation("导入Excel")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<CmsCommunity> util = new ExcelUtil<CmsCommunity>(CmsCommunity.class);
        List<CmsCommunity> customerList = util.importExcel(file.getInputStream());
        String message = communityService.importData(customerList);
        return AjaxResult.success(message);
    }

    /**
     * 导入数据
     */
    @ApiOperation("随机导入")
    @PostMapping("/randImport")
    @ResponseBody
    public AjaxResult randomImportData(MultipartFile file) throws Exception
    {
        ExcelUtil<RandomDto> util = new ExcelUtil<RandomDto>(RandomDto.class);
        List<RandomDto> customerList = util.importExcel(file.getInputStream());
        String message = communityService.randomImportData(customerList);
        return AjaxResult.success(message);
    }

    @ApiOperation(value = "下载模板",produces = "application/octet-stream")
    @PostMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<CmsCommunity> util = new ExcelUtil<CmsCommunity>(CmsCommunity.class);
        return util.importTemplateExcel("客户数据模板");
    }
    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("查看详情")
    @PostMapping("/query")
    @ResponseBody
    public AjaxResult queryById(Long id) {
        return success(communityService.queryById(id));
    }
    /**
     * 编辑数据
     * @param dto 实体
     * @return 编辑结果
     */
    @ApiOperation("保存")
    @CmsLog(title = "团购团长",operatorType = OperatorType.UPDATE )
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestBody CommunityDto dto) {
        return communityService.save(dto)?success():error();
    }


    @ApiOperation("导出Excel")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String userId)
    {
        List<CmsCommunity> list = communityService.selectList(userId,"");
        ExcelUtil<CmsCommunity> util = new ExcelUtil<>(CmsCommunity.class);
        return util.exportExcel( list, "客户数据");
    }

    @ApiOperation("回收任务")
    @CmsLog(title = "团购团长",operatorType = OperatorType.RECYCLE )
    @PostMapping("/recycle")
    @ResponseBody
    public AjaxResult recycleByIds(@RequestBody List<Long> idList)
    {
        return communityService.recycle(idList)?success("回收成功！"):error("回收失败！");
    }

    @ApiOperation("指派任务")
    @CmsLog(title = "团购团长",operatorType = OperatorType.ASSIGN )
    @PostMapping("/assign")
    @ResponseBody
    public AjaxResult assign( @RequestBody List<AssignDto> assignList){
        return communityService.assign(assignList)?success("指派成功！"):error("指派失败！");
    }


    @ApiOperation("工作汇总")
    @PostMapping("/log/list")
    @ResponseBody
    public TableDataInfo logList(int offset)
    {
        startPage();
        List<Object> list = communityService.selectLogList(offset);
        return getDataTable(list);
    }




}

